package cn.edu.zjut.service;

import cn.edu.zjut.dao.CustomerDAO;
import cn.edu.zjut.dao.UserDao;
import cn.edu.zjut.entity.User;
import cn.edu.zjut.po.Address;
import cn.edu.zjut.po.Customer;
import com.opensymphony.xwork2.ActionContext;
import org.hibernate.Transaction;

import java.util.List;
import java.util.Map;

/**
 * @author XiaoFeng
 * @version 1.0
 * @Description Create by 2021/10/27 14:28
 */

public class UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public boolean loginVerify(String username, String password) {
        boolean passLogin = false;
        passLogin = userDao.verifyUsername(username);
        if(passLogin) {
            passLogin = userDao.verifyPassword(username, password);
        }
        return passLogin;
    }

    public boolean userRegister(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return userDao.addUser(user);
    }
    private Map<String, Object> request, session;

    public boolean login(Customer loginUser) {
        ActionContext ctx = ActionContext.getContext();
        session = (Map) ctx.getSession();
        request = (Map) ctx.get("request");
        String account = loginUser.getAccount();
        String password = loginUser.getPassword();
        String hql = "from Customer as user where account='" + account +
                "' and password='" + password + "'";
        CustomerDAO dao = new CustomerDAO();
        List list = dao.findByHql(hql);
        dao.getSession().close();
        if (list.isEmpty()) return false;
        else {
            session.put("user", account);
            request.put("tip", "登陆成功!");
            loginUser = (Customer) list.get(0);
            request.put("loginUser", loginUser);
            return true;
        }
    }

    public boolean register(Customer loginUser) {
        ActionContext ctx = ActionContext.getContext();
        session = (Map) ctx.getSession();
        request = (Map) ctx.get("request");
        CustomerDAO dao = new CustomerDAO();
        Transaction tran = null;
        try {
            tran = dao.getSession().beginTransaction();
            dao.save(loginUser);
            tran.commit();
            session.put("user", loginUser.getAccount());
            request.put("tip", "注册成功!");
            request.put("loginUser", loginUser);
            return true;
        } catch (Exception e) {
            if (tran != null) tran.rollback();
            return false;
        } finally {
            dao.getSession().close();
        }
    }

    public boolean delete(Customer loginUser) {
        ActionContext ctx = ActionContext.getContext();
        session = (Map) ctx.getSession();
        request = (Map) ctx.get("request");
        CustomerDAO dao = new CustomerDAO();
        Transaction tran = null;
        try {
            tran = dao.getSession().beginTransaction();
            dao.delete(loginUser);
            tran.commit();
            session.remove("user");
            request.put("tip", "删除个人信息成功，请重新登录！");
            return true;
        } catch (Exception e) {
            if (tran != null) tran.rollback();
            return false;
        } finally {
            dao.getSession().close();
        }
    }

    public boolean update(Customer loginUser) {
        ActionContext ctx = ActionContext.getContext();
        session = (Map) ctx.getSession();
        request = (Map) ctx.get("request");
        CustomerDAO dao = new CustomerDAO();
        Transaction tran = null;
        try {
            tran = dao.getSession().beginTransaction();
            dao.update(loginUser);
            tran.commit();
            session.remove("user");
            session.put("user", loginUser.getAccount());
            request.put("tip", "更新个人信息成功！");
            return true;
        } catch (Exception e) {
            //回滚
            if (tran != null) tran.rollback();
            return false;
        } finally {
            dao.getSession().close();
        }
    }

    public boolean addAddr(Customer loginUser, Address address) {
        ActionContext ctx = ActionContext.getContext();
        request = (Map) ctx.get("request");
        CustomerDAO c_dao = new CustomerDAO();

        loginUser = (Customer) c_dao.findById(loginUser.getCustomerId());//通过DAO层进行hibernate内置的sql语句生成
        loginUser.setAddress(address);
//        address.setCustomer(loginUser); //一个地址唯一对应一个用户，将该用户信息绑定到地址中
//        loginUser.getAddresses().add(address);//用户的地址集合中添加该地址

        Transaction tran = null;
        try {
            tran = c_dao.getSession().beginTransaction();
            c_dao.update(loginUser);
            tran.commit();
            request.put("loginUser", loginUser);
            request.put("tip", "添加地址成功！");
            return true;
        } catch (Exception e) {
            if (tran != null) tran.rollback();
            return false;
        } finally {
            c_dao.getSession().close();
        }
    }

    public boolean delAddr(Customer loginUser) {
        ActionContext ctx = ActionContext.getContext();
        request = (Map) ctx.get("request");
        CustomerDAO c_dao = new CustomerDAO();
//        AddressDao addressDao = new AddressDao();

        loginUser = (Customer) c_dao.findById(loginUser.getCustomerId());//通过DAO层进行hibernate内置的sql语句生成
        //用户的地址集合中删除该地址
        loginUser.setAddress(null);
//        address = (Address) addressDao.findById(address.getAddressId());
        //从地址对应的多个用户中删除该用户
//        address.getCustomers().remove(loginUser);

        Transaction tran = null;
        try {
            tran = c_dao.getSession().beginTransaction();
            c_dao.update(loginUser);
            tran.commit();

//            tran = addressDao.getSession().beginTransaction();
//            addressDao.update(address);
//            tran.commit();

            request.put("loginUser", loginUser);
//            request.put("address", address);
            request.put("tip", "用户删除地址成功！");
            return true;
        } catch (Exception e) {
            if (tran != null) tran.rollback();
            return false;
        } finally {
            c_dao.getSession().close();
//            addressDao.getSession().close();
        }
    }
}
